package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.OrderBean;
import com.project.bean.TicketBean;
import com.project.bean.UserBean;
import com.project.service.ICinemaService;
import com.project.service.IOrderService;
import com.project.service.IUserService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private ICinemaService cinemaService;
    @Autowired
    private IOrderService orderService;

    /**
     * 登录
     *
     * @param username
     * @param password
     * @param request
     * @return
     */
    @RequestMapping("login")
    public String login(String username, String password, HttpServletRequest request) {
        UserBean userBean = userService.login(username, password);
        if (userBean != null) {
            request.getSession().setAttribute("loginUser", userBean);
            return "ok";
        } else {
            return "no";
        }
    }

    /**
     * 分页查找电影票
     *
     * @param pageNO
     * @param movieName
     * @param cinema
     * @param seriesName
     * @param row
     * @return
     */
    @RequestMapping("findByItem")
    public Page<TicketBean> findByItem(Integer pageNO,
                                       String movieName, String cinema,
                                       String seriesName, String row) {
        return cinemaService.findByItem(pageNO, movieName, cinema, seriesName, row);
    }

    /**
     * 当前登录用户下订单
     * @param movieName
     * @param seriesNum
     * @param cinema
     * @param row
     * @param seat
     * @param request
     * @return
     */
    @RequestMapping("addOrder")
    public String addOrder(String movieName, String seriesNum,
                         String cinema,
                         String row,
                         Integer seat, HttpServletRequest request) {
        UserBean userBean = (UserBean) request.getSession().getAttribute("loginUser");
        orderService.addOrder(userBean.getUserName(), movieName, seriesNum, cinema, row, seat);
        return "ok";
    }

    /**
     * 通过当前登录用户查找该用户的订单
     * @param request
     * @return
     */
    @RequestMapping("getOrderAll")
    public List<OrderBean> getOrderAll( HttpServletRequest request) {
        UserBean userBean = (UserBean) request.getSession().getAttribute("loginUser");
        return orderService.getOrderAll(userBean.getUserName());
    }

}
